################################################################################
##########################    Data Preparation   ###############################
################################################################################
library(labelled)
library(dplyr)
library(car)
options(max.print = 99999)
rm(list=ls())

setwd("")
load("LAPOP_PELA_VDEM_ILO.RData")
base=LAPOP_PELA_VDEM_ILO

###########################    Level-1 variables    ############################

base=remove_var_label(base)
## Id lapop survey##
base$id_lapop <- paste(base$paisR, base$wave, sep = "")
table(base$id_lapop)

### Political trust  ###
#Trust in the justice system (1=Not at all, 7=Much)
base$conf_justicia=as.numeric(as.character(base$b10a))
#Trust in the electoral system (1=Not at all, 7=Much)
base$conf_electoral=as.numeric(as.character(base$b11))
#Trust in the armed forces (1=Not at all, 7=Much)
base$conf_ffaa=as.numeric(as.character(base$b12))
#Trust in the parliament (1=Not at all, 7=Much)
base$conf_parlamnt=as.numeric(as.character(base$b13))
#Trust in the government (1=Not at all, 7=Much)
base$conf_gobierno=as.numeric(as.character(base$b14))
#Trust in the President (1=Not at all, 7=Much)
base$conf_presi=as.numeric(as.character(base$b21a))
#Trust in political parties (1=Not at all, 7=Much)
base$conf_partidos=as.numeric(as.character(base$b21))
#Trust in the police (1=Not at all, 7=Much)
base$conf_policia=as.numeric(as.character(base$b18))
#Trust in the catholic church (1=Not at all, 7=Much)
base$conf_iglesia=as.numeric(as.character(base$b20))


#Vote in last presidential elections
base$voto_presid=as.numeric(as.character(car::recode(base$vb2, "1=1; 2=0")))

## Identificaction with a political party ##
table(base$vb10)
base$id_partido=as.numeric(as.character(car::recode(base$vb10, "1=1; 2=0")))

##Ideological scale (1=Left, 10=Right)
base$izqder=as.numeric(as.character(base$l1))
base$izqder=car::recode(base$izqder, "NA=99")
table(base$izqder)

##Left ideology (Barnes y Saxton, 2019)
base$izq = as.numeric(as.character(car::recode(base$izqder, "1:3=1; 4:10=0; 99=0")))
base$izq = ifelse(base$id_lapop =='Rep. Dominicana2004', NA, base$izq)
table(base$izq)

##self-positioning on the ideological scale
base$id_ideo=as.numeric(as.character(car::recode(base$izqder, "1:10=1; 99=0")))
base$id_ideo = ifelse(base$id_lapop =='Rep. Dominicana2004', NA, base$id_ideo)
table(base$id_ideo)

#Social Trust
base$conf_interpers=as.numeric(as.character(car::recode(base$it1, "1=4; 2=3; 3=2; 4=1")))

##Life satisfaction
base$satvida=as.numeric(as.character(car::recode(base$ls3, "1=4; 2=3; 3=2; 4=1")))

#Perception of corruption (1=Not at all generalized, 4=Very generalized)
base$percep_corrup=as.numeric(as.character(car::recode(base$exc7, "1=4; 2=3; 3=2; 4=1")))

#To what extent does the government combat corruption
base$comb_corrup <- as.numeric(base$n9)

#economic assessment of the country (as dummy)
base$eval_econpais=as.numeric(as.character(car::recode(base$soct2, "1=3; 2=2; 3=1")))
base$eval_econpais_Dummy = car::recode(base$eval_econpais, "3=1; 1:2=0")

###  Sociodemographics  ###

#urban
base$urbano=as.numeric(as.character(car::recode(base$ur, "1=1; 2=0")))
#sexo (1=female, 0=male)
base$mujer=as.numeric(as.character(car::recode(base$q1, "1=0; 2=1")))
#age
base$edad=as.numeric(as.character(base$q2))
#Education (continuous)
base$educacion=as.numeric(as.character(base$ed))
summary(base$educacion)
#Tertiary education
base$eduniv=car::recode(base$educacion, "1:12=0; 13:18=1")
base$edunivF=as.factor(car::recode(base$educacion, "1:12=0; 13:18=1"))

#marital status
base$estcivil=as.numeric(as.character(base$q11))
base$estcivil2=as.numeric(as.character(base$q11n))
base$estadocivil=if_else(is.na(base$estcivil), base$estcivil2, base$estcivil)
base$casado=as.numeric(as.character(car::recode(base$estadocivil, "1=0; 2=1; 3=1;
                                                4=0; 5=0; 6=0; 7=1")))


#############################    Select variables    ###########################

lapop_pela_select=dplyr::select(
  base, id_pela, nleg, id_encuestado,id_lapop,paisR,year,inicio_lapop,fin_lapop, 
  combina, wave,idnum, idnum_1418, estratopri,estratosec,strata,upm,provincia,
  prov, municipio,municipio04,municipio06,municipio08,municipio10,cluster,ur,
  tamano,idiomaq, fecha,wt, weight1500,conf_justicia,conf_electoral,conf_ffaa,
  conf_parlamnt, conf_gobierno,conf_presi,conf_policia,conf_iglesia,conf_partidos,
  voto_presid, id_partido, izqder, izq, id_ideo,eval_econpais_Dummy, conf_interpers, 
  satvida, percep_corrup, comb_corrup, eval_econpais, eval_econpais_Dummy,urbano, 
  mujer, edad, educacion, eduniv, edunivF, estadocivil, casado, Fecha_Inicio,
  Fecha_termino, recambio_media,recambio_media_w ,familiar_pol_media, 
  familiar_pol_media_w, ideolegis_media, ideolegis_media_w, ideolegis_sd,
  ideolegis_sd_w, edad_media, edad_media_w ,edad_sd, edad_sd_w, educ_media,
  educ_media_w,educ_sd,educ_sd_w, sexo_media,sexo_media_w,sexo_sd,sexo_sd_w,
  workers.prop, workers.prop_w, univ_media,univ_media_w, edpadre_media, 
  edpadre_media_w,edpadre_sd,edpadre_sd_w, padreuniv,padreuniv_w, estado_mer_media, 
  estado_mer_media_w,estado_mer_sd, estado_mer_sd_w, country_name, v2x_polyarchy, 
  v2x_corr,e_gdppc,e_peaveduc, e_peedgini,e_miurbani,e_miinflat,e_pop, e_miurbpop,
  desempleo)


#Remove rows without LAPOP information
lapop_pela_select=dplyr::filter(lapop_pela_select, !is.na(lapop_pela_select$mujer))
#Remove rows without PELA information
lapop_pela_select=dplyr::filter(lapop_pela_select, !is.na(lapop_pela_select$id_pela))
lapop_pela_select$wave= remove_labels(lapop_pela_select$wave)
#Create country ID (paisF)
lapop_pela_select$paisF=as.factor(lapop_pela_select$paisR)
table(lapop_pela_select$paisF)
#Create country-year ID (pais.ano)
lapop_pela_select$pais.ano = paste0(lapop_pela_select$paisR, lapop_pela_select$wave)
table(lapop_pela_select$pais.ano)
#Create individual ID
lapop_pela_select$id_unico <-c(1:nrow(lapop_pela_select))

save(lapop_pela_select, file="lapop_pela_select.RData")
load("lapop_pela_select.RData")
